﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Common.Utils;
using PowerCreator.LMS.User.Entity;
using PowerCreator.LMS.Web.User;
using PowerCreator.LMS.Web.DL.Project;
using PowerCreator.LMS.DL.Project.Entity;


public partial class Theme_DL_EditPWD : PowerCreator.LMS.Web.Base.BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            InitPage();
            this.btnSave.Attributes.Add("OnClick", "return checkForm()");
            //int uid = GetLoginEntity.UserID;
            //User objUser = new User();
            //如果是超级管理员帐户或者内置帐户不允许修改个人密码
            if (GetLoginEntity.AccountType == PowerCreator.LMS.Web.DL.User.LoginUserType.SuperAdministartor || GetLoginEntity.AccountType == PowerCreator.LMS.Web.DL.User.LoginUserType.InnerUser)
            {
                txt_old_PassWord.ReadOnly = true;
                txt_new_PassWord.ReadOnly = true;
                txt_db_PassWord.ReadOnly = true;
            }
        }

    }
    protected override void InitPage()
    {
        //lbl_InfoTitle_EditorPassword.Text = GetLanageValueByKey("EditorPassword_desc1", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnSave.Text = GetLanageValueByKey("btn_Save", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnReset.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        base.InitPage();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        int uid = GetLoginEntity.UserID;

        if (GetLoginEntity.Password != MD5Helper.GetMD5(txt_old_PassWord.Text.Trim()))
        {
            lbl_msg_Error.Text = "旧密码错误,请重新输入!";
            txt_old_PassWord.Focus();
            return;
        }
        else
        {
            if (string.IsNullOrEmpty(txt_new_PassWord.Text.Trim()))
            {
                lbl_msg_Error.Text = "新密码不能为空!请输入!";
                return;
            }
            if (txt_new_PassWord.Text.Trim().Length < 6 || txt_new_PassWord.Text.Trim().Length > 20)
            {
                lbl_msg_Error.Text = "密码长度为 6-20 个字符!请重新输入!";
                return;
            }

            if (!txt_new_PassWord.Text.Trim().Equals(txt_db_PassWord.Text.Trim()))
            {
                lbl_msg_Error.Text = "两次的密码不一致!请重新输入!";
                return;
            }


            TOLearner objLearner = new TOLearner();
            //PowerCreator.LMS.Web.CreatedByInfo cinfo = GetCreatedByInfo();
            VOTOLearner vo = new VOTOLearner();
            vo = objLearner.Load(uid);
            string newPassword = MD5Helper.GetMD5(txt_new_PassWord.Text.Trim());
            vo.UserPwd = newPassword;

            if (objLearner.Update(vo))
            {
                GetLoginEntity.VOLearner.UserPwd = newPassword;
                GetLoginEntity.Password = newPassword;
                //Script.Alert("修改成功！");
                string url = "StudyCenter.aspx";
                Script.ShowAndRedirect(this, "修改成功！", url);
            }
            else
            {
                lbl_msg_Error.Text = "修改用户密码失败!";
            }
        }
    }
    protected void btnReset_Click(object sender, EventArgs e)
    {
        string url = "Default.aspx";
        Script.RedirectURL(url);
    }
}
